package ua.org.nuos.sdms.clientgui.client.handlers;

import ua.org.nuos.sdms.clientgui.client.context.Messages;
import ua.org.nuos.sdms.clientgui.client.controller.GlobalBoard;
import ua.org.nuos.sdms.clientgui.client.events.ErrorMessageEvent;
import ua.org.nuos.sdms.clientgui.client.events.InfoMessageEvent;
import ua.org.nuos.sdms.clientgui.client.events.UpdateGroupEvent;
import ua.org.nuos.sdms.clientgui.client.listeners.UpdateGroupListener;
import ua.org.nuos.sdms.clientgui.server.AppProxy;
import ua.org.nuos.sdms.middle.entity.Group;
import ua.org.nuos.sdms.middle.util.exception.UpdateEntityException;

import java.util.logging.Level;
import java.util.logging.Logger;

/**
 * Created by IntelliJ IDEA.
 * User: dio
 * Date: 27.03.12
 * Time: 19:56
 * To change this template use File | Settings | File Templates.
 */
public class UpdateGroupHandler implements UpdateGroupListener {
    @Override
    public void update(UpdateGroupEvent event) {
        try {
            Group group = event.getGroup();
            AppProxy.getInstance().getServiceLocator().getClientBean().updateGroup(group);
            Group updatedGroup = AppProxy.getInstance().getServiceLocator().getClientBean().findGroupById(group.getId());
            AppProxy.getInstance().getModelLocator().setGroup(updatedGroup);
            AppProxy.getInstance().getControllerLocator().getController(GlobalBoard.ID).fire(new InfoMessageEvent(Messages.UPDATE_SUCCESSFULLY));
        } catch (UpdateEntityException e) {
            Logger.getAnonymousLogger().log(Level.WARNING, "Update group error", e);
            AppProxy.getInstance().getControllerLocator().getController(GlobalBoard.ID).fire(new ErrorMessageEvent(Messages.UPDATE_GROUP_ERROR));
        } catch (Exception ex) {
            Logger.getAnonymousLogger().log(Level.WARNING, "Update group error", ex);
            AppProxy.getInstance().getControllerLocator().getController(GlobalBoard.ID).fire(new ErrorMessageEvent(Messages.ACTION_ERROR));
        }
    }
}
